#!/usr/bin/env bash

# Runs flyway and whatever provided command against the MISO DB that is set up with this container
# Relies on environment variables: MISO_DB_PASS_FILE, MISO_DB_USER, MISO_DB_HOST_PORT, MISO_DB, MISO_FILES_DIR
# the first argument given to this script is passed fully onto the flyway command
# e.g.  
#   run-flyway migrate : applies all of the migrations on the classpath and in /flyway/migrations
#   run-flyway clean : rolls back changes

password=$(cat ${MISO_DB_PASS_FILE})

command="flyway -user='${MISO_DB_USER}' -password='${password}' \
  -url='jdbc:mysql://${MISO_DB_HOST_PORT}/${MISO_DB}?autoReconnect=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=UTF-8&useSSL=false' \
  -locations='filesystem:/flyway/sql,classpath:uk.ac.bbsrc.tgac.miso.db.migration' -outOfOrder=true -placeholders.filesDir='${MISO_FILES_DIR}' -connectRetries=60"

eval "${command} $1"

rc=$?; if [[ $rc != 0 ]]
then
	"run-flyway command $1 failed with exit : $rc"
fi
exit $rc; 

